package com.singzi.leetcode.jianzhiOffer2;

public class T0009 {
    public static void main(String[] args) {
        Solution0009 solution0009 = new Solution0009();
        int []nums = {10,5,2,6};
        int k = 1000;
        System.out.println(solution0009.numSubarrayProductLessThanK(nums, k));
    }
}
class Solution0009 {
    public int numSubarrayProductLessThanK(int[] nums, int k) {
        int left = 0, right = 0;
        int ans = 0;
        int product = nums[right];
        int temp = 0;
        while(right < nums.length && left < nums.length){
//            right = Math.max(left, right);
            while(product < k && right < nums.length){
                right++;
                if(right < nums.length) product *= nums[right];
            }
            ans += (temp - left)*(right - temp) + (right - temp + 1)*(right - temp)/2;
            temp = right;
            product /= nums[left];
            left++;
        }
        return ans;
    }
}